
package quexi.components
{
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	
	public class ListItem extends Sprite
	{
		private static const DEFULT_COLOR:uint 	= 0xFFFFFF;
		private static const OVER_COLOR:uint 	= 0xFFF0FF;
		private static const SELECT_COLOR:uint 	= 0xFFE6FF;
		private static const BORDER_COLOR:uint	= 0xFF66FF;
		
		private var _data:Object;
		private var _width:Number = 0;
		private var _height:Number = 0;
		private var _label:TextField;
		private var _selected:Boolean;
		private var _isMouseOver:Boolean;
		
		public function ListItem(itemWidth:Number, itemHeight:Number)
		{
			this.width	= itemWidth;
			this.height = itemHeight;
			render();
			addEventListener(MouseEvent.MOUSE_OVER, onMouseOver);
		}
		
		/**
		 * 渲染 
		 */		
		private function render():void
		{
			graphics.clear();
			
			if(_selected)
			{//被选中的时候
				graphics.lineStyle(2,BORDER_COLOR);
				graphics.beginFill(SELECT_COLOR);
			}
			else if(_isMouseOver)
			{//鼠标移入但未选中
				graphics.lineStyle(2,BORDER_COLOR);
				graphics.beginFill(OVER_COLOR);
			}
			else
			{//默认状态
				graphics.beginFill(DEFULT_COLOR);
			}
			
			graphics.drawRect(0,0,width,height);
			graphics.endFill();
			
			renderData();
			

		}
		
		/**
		 * 渲染显示内容 
		 */		
		private function renderData():void
		{
			if(_label == null)
			{
				_label = new TextField();
				_label.autoSize 	= TextFieldAutoSize.LEFT;
				_label.selectable 	= false;
				_label.mouseEnabled = false;
				_label.x 			= 10;
				
				addChild(_label);
			}
			
			if(_data != null)
			{
				_label.text = _data.label;
			}
		}
		
		/////////////////////
		//event handler
		/////////////////////
		
		private function onMouseOver(e:MouseEvent):void
		{
			addEventListener(MouseEvent.MOUSE_OUT, onMouseOut);
			_isMouseOver = true;
			render();
		}
		
		private function onMouseOut(e:MouseEvent):void
		{
			removeEventListener(MouseEvent.MOUSE_OUT, onMouseOut);
			_isMouseOver = false;
			render();
		}
		
		
		///////////////
		//overrides
		///////////////
		
		override public function get width():Number
		{
			return _width;
		}
		
		override public function set width(value:Number):void
		{
			_width = value;	
		}
		
		override public function get height():Number
		{
			return _height;
		}
		
		override public function set height(value:Number):void
		{
			_height = value;	
		}

		
		public function get selected():Boolean
		{
			return _selected;
		}

		public function set selected(value:Boolean):void
		{
			_selected = value;
			render();
		}

		public function get data():Object
		{
			return _data;
		}

		public function set data(value:Object):void
		{
			_data = value;
			render();
		}


		//
	}
}